//
//  MusicPlayerPlayView.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/10/20.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "PlayBottomView.h"

@interface PlayBottomView (){

    void (^MusicPlayerPlayTypeAction)(MusicPlayType playType);
    void (^MusicPlayerPlayPrevAction)();
    void (^MusicPlayerPlayAction)(BOOL isPlaying);
    void (^MusicPlayerPlayNextAction)();
    void (^MusicPlayerPlayMenuAction)();
    
    BOOL isPlay;
   
    
}

@property (nonatomic,strong) UIButton *playTypeButton;
@property (nonatomic,strong) UIButton *prevButton;
@property (nonatomic,strong) UIButton *playButton;
@property (nonatomic,strong) UIButton *nextButton;
@property (nonatomic,strong) UIButton *menuButton;

@end

@implementation PlayBottomView


+ (instancetype)playBottomViewOffsetY:(CGFloat)offsetY{

    PlayBottomView *bottom=[[PlayBottomView alloc] initWithFrame:CGRectMake(0, offsetY, [UIScreen mainScreen].bounds.size.width, 66)];
    
    return bottom;
}

- (instancetype)initWithFrame:(CGRect)frame{

    if (self=[super initWithFrame:frame]) {
        
        self.backgroundColor=[UIColor colorWithRed:50/255.0 green:208/255.0 blue:47/255.0 alpha:1.0];
        
        isPlay=NO;
        _musicPlayType=MusicPlayQueueType;
        
        [self addSubview:self.playTypeButton];
        [self addSubview:self.playButton];
        [self addSubview:self.prevButton];
        [self addSubview:self.nextButton];
        [self addSubview:self.menuButton];
        
    }
    return self;
}

#pragma mark -事件
- (void)musicButtonClick:(UIButton *)sender{


    if (self.playTypeButton==sender) {
        
        if (_musicPlayType==MusicPlayQueueType) {
            _musicPlayType=MusicPlayLoopType;
            
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_one"] forState:UIControlStateNormal];
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_one_prs"] forState:UIControlStateHighlighted];
            
        }else if (_musicPlayType==MusicPlayLoopType){
            _musicPlayType=MusicPlayRandomType;
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_shuffle"] forState:UIControlStateNormal];
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_shuffle_prs"] forState:UIControlStateHighlighted];
        }else{
            _musicPlayType=MusicPlayQueueType;
            
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_loop"] forState:UIControlStateNormal];
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_loop_prs"] forState:UIControlStateHighlighted];
        }
        
        if (MusicPlayerPlayTypeAction) {
            MusicPlayerPlayTypeAction(_musicPlayType);
        }
    }else if (self.prevButton==sender){
        if (MusicPlayerPlayPrevAction) {
            MusicPlayerPlayPrevAction();
        }
    }else if (self.playButton==sender){

        [self setPlayStatus:!isPlay];
        
        if (MusicPlayerPlayAction) {
            MusicPlayerPlayAction(isPlay);
        }
    }else if (self.nextButton==sender){
        if (MusicPlayerPlayNextAction) {
            MusicPlayerPlayNextAction();
        }
    }else if (self.menuButton==sender){
        if (MusicPlayerPlayMenuAction) {
            MusicPlayerPlayMenuAction();
        }
    }
    
}

/**
 *  按钮事件
 *
 *  @param orderAction 播放类别动作
 *  @param prevAction  上一首
 *  @param playAction  播放
 *  @param nextAction  下一首
 *  @param menuAction  功能项
 */
- (void)setType:(void (^) (MusicPlayType playType))orderAction
           prev:(void (^) ())prevAction
           play:(void (^) (BOOL playing))playAction
           next:(void (^) ())nextAction
           menu:(void (^) ())menuAction{

    MusicPlayerPlayTypeAction=orderAction;
    MusicPlayerPlayPrevAction=prevAction;
    MusicPlayerPlayAction=playAction;
    MusicPlayerPlayNextAction=nextAction;
    MusicPlayerPlayMenuAction=menuAction;
}

/**
 *  设置播放状态
 *
 *  @param status YES:播放 NO:停止
 */
- (void)setPlayStatus:(BOOL)status{
    if (isPlay!=status) {
        isPlay=status;
        if (status) { //播放
            [self.playButton setImage:[UIImage imageNamed:@"cm2_btn_pause"] forState:UIControlStateNormal];
            [self.playButton setImage:[UIImage imageNamed:@"cm2_btn_pause_prs"] forState:UIControlStateHighlighted];
        }else{
            [self.playButton setImage:[UIImage imageNamed:@"cm2_btn_play.png"] forState:UIControlStateNormal];
            [self.playButton setImage:[UIImage imageNamed:@"cm2_btn_play_prs.png"] forState:UIControlStateHighlighted];
        }
    }
    
   
}

/**
 *  设置播放类型
 *
 *  @param playType 播放类型
 */
- (void)setMusicPlayType:(MusicPlayType)playType{
    if (_musicPlayType!=playType) {
        _musicPlayType=playType;
        
        if (_musicPlayType==MusicPlayQueueType) {
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_loop"] forState:UIControlStateNormal];
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_loop_prs"] forState:UIControlStateHighlighted];
        }else if (_musicPlayType==MusicPlayLoopType){
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_one"] forState:UIControlStateNormal];
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_one_prs"] forState:UIControlStateHighlighted];
        }else{
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_shuffle"] forState:UIControlStateNormal];
            [self.playTypeButton setImage:[UIImage imageNamed:@"cm2_play_btn_shuffle_prs"] forState:UIControlStateHighlighted];
        }
    }
}

#pragma mark -懒加载
- (UIButton *)playButton{

    if (!_playButton) {
        
        UIImage *normalImg=[UIImage imageNamed:@"cm2_btn_play.png"];
        _playButton=[self createButtonWithFrame:CGRectMake((self.bounds.size.width-normalImg.size.width)/2, (self.bounds.size.height-normalImg.size.height)/2, normalImg.size.width, normalImg.size.height) normalImage:@"cm2_btn_play.png" highlightImage:@"cm2_btn_play_prs.png" disableImage:nil];
    }
    
    return _playButton;
}

- (UIButton *)playTypeButton{
    
    if (!_playTypeButton) {
        
        UIImage *normalImg=[UIImage imageNamed:@"cm2_play_btn_loop.png"];
        _playTypeButton=[self createButtonWithFrame:CGRectMake(8, (self.bounds.size.height-normalImg.size.height)/2, normalImg.size.width, normalImg.size.height) normalImage:@"cm2_play_btn_loop.png" highlightImage:@"cm2_play_btn_loop_prs.png" disableImage:nil];
    }
    
    return _playTypeButton;
}

- (UIButton *)prevButton{
    
    if (!_prevButton) {

        UIImage *normalImg=[UIImage imageNamed:@"cm2_play_btn_prev.png"];

        CGFloat leftx=CGRectGetMaxX(self.playTypeButton.frame)+(self.playButton.frame.origin.x-CGRectGetMaxX(self.playTypeButton.frame)-normalImg.size.width)/2;
        
        _prevButton=[self createButtonWithFrame:CGRectMake(leftx,(self.bounds.size.height-normalImg.size.height)/2, normalImg.size.width, normalImg.size.height) normalImage:@"cm2_play_btn_prev.png" highlightImage:@"cm2_play_btn_prev_prs.png" disableImage:nil];
    }
    
    return _prevButton;
}

- (UIButton *)nextButton{
    
    if (!_nextButton) {
        
        UIImage *normalImg=[UIImage imageNamed:@"cm2_play_btn_next.png"];
        
        CGFloat leftx=CGRectGetMaxX(self.playButton.frame)+(self.playButton.frame.origin.x-CGRectGetMaxX(self.playTypeButton.frame)-normalImg.size.width)/2;
        
        _nextButton=[self createButtonWithFrame:CGRectMake(leftx,(self.bounds.size.height-normalImg.size.height)/2, normalImg.size.width, normalImg.size.height) normalImage:@"cm2_play_btn_next.png" highlightImage:@"cm2_play_btn_next_prs.png" disableImage:nil];
    }
    
    return _nextButton;
}

- (UIButton *)menuButton{
    
    if (!_menuButton) {
        
        UIImage *normalImg=[UIImage imageNamed:@"cm2_play_btn_src.png"];
        
        _menuButton=[self createButtonWithFrame:CGRectMake(self.bounds.size.width-normalImg.size.width-self.playTypeButton.frame.origin.x,(self.bounds.size.height-normalImg.size.height)/2, normalImg.size.width, normalImg.size.height) normalImage:@"cm2_play_btn_src.png" highlightImage:@"cm2_play_btn_src_prs.png" disableImage:@"cm2_play_btn_src_dis.png"];
    }
    
    return _menuButton;
}

- (UIButton *)createButtonWithFrame:(CGRect)frame normalImage:(NSString *)normalImg highlightImage:(NSString *)highlightImage disableImage:(NSString *)disableImage{

    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=frame;
    if (normalImg&&[normalImg length]>0) {
        [btn setImage:[UIImage imageNamed:normalImg] forState:UIControlStateNormal];
    }
    if (highlightImage&&[highlightImage length]>0) {
        [btn setImage:[UIImage imageNamed:highlightImage] forState:UIControlStateHighlighted];
    }
    if (disableImage&&[disableImage length]>0) {
        [btn setImage:[UIImage imageNamed:disableImage] forState:UIControlStateDisabled];
    }
    [btn addTarget:self action:@selector(musicButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

@end
